/*
 * Copyright (C) 2014 VenomVendor <info@VenomVendor.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.naizhuai.ui.adapter.business;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.naizhuai.R;
import com.naizhuai.ui.components.CircleImageView;
import com.naizhuai.ui.entity.mainbusiness.TargetBean;
import com.naizhuai.ui.entity.mainbusiness.TargetDetailsReplayBean;
import com.naizhuai.ui.entity.mainbusiness.TaskScoreBean;
import com.naizhuai.util.ImageUtils;
import com.naizhuai.util.MathUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TargetReviewAdapterHolder extends RecyclerView.Adapter {

    private Map<Integer, TaskScoreBean> points;
    private StarSelectedListener mListener;
    private onItemClickListener listener;


    private List<TargetDetailsReplayBean.TaskBean> taskList;
    private Context mContext;

    public TargetReviewAdapterHolder(Context context, List<TargetDetailsReplayBean.TaskBean> taskList) {
        this.mContext = context;
        this.taskList = taskList;
        points=new HashMap<>();
    }

    public void setStarSelectedListener(StarSelectedListener mListener) {
        this.mListener = mListener;
    }

    public void setOnItemClickListener(onItemClickListener listener){
        this.listener=listener;
    }


    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View sView = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_target_review_item_layout, parent, false);
        return new TargetItemHolder(sView);
    }


    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        TargetItemHolder manageHolder = (TargetItemHolder) holder;
        ImageUtils.glideThumbViewByUrl(mContext,manageHolder.userThumb,taskList.get(position).getManagerUserInfo().getThumb());

//        //组织人数
//        String teamNumber = String.format(mContext.getText(R.string.organization_person_number_format).toString(), taskList.get(position).());
//        //参与人数
//        String joinedNumber = String.format(mContext.getText(R.string.joined_number_format).toString(), taskList.get(position).);
//        //预算
//        String budget = String.format(mContext.getText(R.string.budget_money_format).toString(), MathUtils.getMoneyChange(taskList.get(position)));
        List<TargetDetailsReplayBean.LabelsAndValue> comInfo=taskList.get(position).getTaskCompleteInfo();
        manageHolder.targetName.setText(taskList.get(position).getTaskName());
        manageHolder.teamLabel.setText(comInfo.get(0).getLabel());
        manageHolder.teamNumber.setText(comInfo.get(0).getValue());
        manageHolder.joinedLabel.setText(comInfo.get(1).getLabel());
        manageHolder.joinedNumber.setText(comInfo.get(1).getValue());
        manageHolder.budget.setText(comInfo.get(2).getLabel());
        manageHolder.budgetNumber.setText(comInfo.get(2).getValue());

        for (int index = 0; index < manageHolder.imageViews.length; index++) {
            manageHolder.imageViews[index].setOnClickListener(new StarClickListener(index, position, manageHolder.imageViews));
        }
    }


    @Override
    public int getItemCount() {
        return taskList.size();
    }

    public List<TaskScoreBean> getPoints() {
        List<TaskScoreBean> beans = new ArrayList<>();
        if (points.size() < taskList.size())
            return null;
        beans.addAll(points.values());
        return beans;
    }

    public class StarClickListener implements View.OnClickListener {
        private int point;
        private int groupPosition;
        private ImageView[] startImages;

        public StarClickListener(int point, int groupPosition, ImageView[] startImages) {
            this.point = point;
            this.groupPosition = groupPosition;
            this.startImages = startImages;
        }

        @Override
        public void onClick(View v) {
            for (int index = 0; index < startImages.length; index++) {
                if (index <= point)
                    startImages[index].setImageResource(R.mipmap.star_good);
                else
                    startImages[index].setImageResource(R.mipmap.star_bad);
            }
            points.put(groupPosition, new TaskScoreBean(taskList.get(groupPosition).getTaskId(), point + 1 + ""));
            int total = 0;

            for (TaskScoreBean point : points.values()) {
                total = total + Integer.parseInt(point.getScore());
            }
            if (mListener != null)
                mListener.selectedPointTotal(total);
        }
    }


    public class TargetItemHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
        CircleImageView userThumb;
        TextView targetName, teamLabel,teamNumber, joinedLabel,joinedNumber, budget,budgetNumber,details;
        ImageView[] imageViews;

        public TargetItemHolder(View view) {
            super(view);
            userThumb = (CircleImageView) view.findViewById(R.id.user_thumb);
            targetName = (TextView) view.findViewById(R.id.target_name);
            teamLabel = (TextView) view.findViewById(R.id.team_label);
            teamNumber = (TextView) view.findViewById(R.id.team_number);
            joinedLabel = (TextView) view.findViewById(R.id.joined_label);
            joinedNumber = (TextView) view.findViewById(R.id.joined_number);
            budget = (TextView) view.findViewById(R.id.budget);
            budgetNumber = (TextView) view.findViewById(R.id.budget_number);
            details = (TextView) view.findViewById(R.id.details);
            imageViews = new ImageView[]{view.findViewById(R.id.star_0), view.findViewById(R.id.star_1), view.findViewById(R.id.star_2)
                    , view.findViewById(R.id.star_3), view.findViewById(R.id.star_4)};
            details.setOnClickListener(this);

        }

        @Override
        public void onClick(View v) {
            if(listener!=null)
                listener.onItemClick(getAdapterPosition(),v);
        }
    }

    public interface onItemClickListener{
        void onItemClick(int position,View view);
    }

    public interface StarSelectedListener {
        void selectedPointTotal(int total);
    }
}
